package edu.stanford.bmir.protege.web.server.stkosutil.merger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.Map.Entry;

import org.semanticweb.owlapi.model.AddAxiom;
import org.semanticweb.owlapi.model.EntityType;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyChange;
import org.semanticweb.owlapi.model.RemoveAxiom;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;

import edu.stanford.bmir.protege.web.client.rpc.data.EntityData;
import edu.stanford.bmir.protege.web.client.rpc.data.SubclassEntityData;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.MetaItemList;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.UtilConstants;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.UtilEntityType;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.enricher.CandidateEntity;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.merger.MergenceContent;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.merger.MergenceOptions;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.merger.MergenceResult;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.merger.actions.MergeEntityResult;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.statistics.StatisticsResult;
import edu.stanford.bmir.protege.web.server.change.ChangeApplicationResult;
import edu.stanford.bmir.protege.web.server.change.ChangeDescriptionGenerator;
import edu.stanford.bmir.protege.web.server.change.FixedMessageChangeDescriptionGenerator;
import edu.stanford.bmir.protege.web.server.change.OntologyChangeList;
import edu.stanford.bmir.protege.web.server.owlapi.AssertedClassHierarchyProvider;
import edu.stanford.bmir.protege.web.server.owlapi.OWLAPIProject;
import edu.stanford.bmir.protege.web.server.owlapi.OWLAPIProjectManager;
import edu.stanford.bmir.protege.web.server.owlapi.RenderingManager;
import edu.stanford.bmir.protege.web.server.stkosutil.enricher.CandidateClassHolder;
import edu.stanford.bmir.protege.web.server.stkosutil.enricher.CandidateClassesChangeGenerator;
import edu.stanford.bmir.protege.web.server.stkosutil.enricher.CandidateIndividualsChangeGenerator;
import edu.stanford.bmir.protege.web.server.stkosutil.mapper.OwlClassHolder;
import edu.stanford.bmir.protege.web.server.stkosutil.mapper.OwlClassHolderComparator;
import edu.stanford.bmir.protege.web.server.stkosutil.merger.MergedClassHolder;
import edu.stanford.bmir.protege.web.server.stkosutil.statistics.EntityCounter;
import edu.stanford.bmir.protege.web.shared.DataFactory;
import edu.stanford.bmir.protege.web.shared.project.ProjectId;
import edu.stanford.bmir.protege.web.shared.user.UserId;

public class ClassMerger {
	private ArrayList<String> _existingO1ClassBrowserTexts = null;
	private ArrayList<String> _existingO1IndividualBrowserTexts =  null;
	private HashMap<String, MergeEntityResult> _hEntityName2Result = null;
	private OWLAPIProject getProject(String projectName) {
		if (projectName == null) {
			throw new NullPointerException("projectName must not be null");
		}
		ProjectId projectId = ProjectId.get(projectName);
		// TODO: Log
		return getProject(projectId);
	}

	/**
	 * Gets the OWLAPIProject for a given {@link ProjectId}.If a project with
	 * the specified id exists then that project will be returned, otherwise, a
	 * fresh project will be created and that fresh project returned.
	 * 
	 * @param projectId
	 *            The id of the project.
	 * @return The OWL API project. Not <code>null</code>.
	 */
	private OWLAPIProject getProject(ProjectId projectId) {
		OWLAPIProjectManager pm = OWLAPIProjectManager.getProjectManager();
		return pm.getProject(projectId);
	}

	// *******************

	private List<MergedClassHolder> getSubclasses(OWLAPIProject project,
			String className) {
		if (className == null) {
			return Collections.emptyList();
		}
		List<MergedClassHolder> result = new ArrayList<MergedClassHolder>();

		RenderingManager rm = project.getRenderingManager();
		AssertedClassHierarchyProvider hierarchyProvider = project
				.getClassHierarchyProvider();
		OWLClass cls = rm.getEntity(className, EntityType.CLASS);

		boolean checkForDeprecated = project.getRootOntology()
				.containsAnnotationPropertyInSignature(
						OWLRDFVocabulary.OWL_DEPRECATED.getIRI());
		for (OWLClass subclass : new ArrayList<OWLClass>(
				hierarchyProvider.getChildren(cls))) {
			boolean deprecated = false;
			// if(checkForDeprecated) {
			// deprecated = project.isDeprecated(subclass);
			// }
			// if (!deprecated) {
			// Set<OWLClass> children = hierarchyProvider.getChildren(subclass);
			// int subClassSubClassesCount = children.size();
			String browserText = rm.getBrowserText(subclass);
			String name = subclass.getIRI().toString();
			MergedClassHolder classHolder = new MergedClassHolder();
			;
			classHolder.setBrowserText(browserText);
			classHolder.setName(name);
			classHolder.setOwlClass(subclass);
			// data.setDeprecated(deprecated);

			// data.setValueType(ValueType.Cls);
			result.add(classHolder);
			// }
		}
		Collections.sort(result, new Comparator<MergedClassHolder>() {
			public int compare(MergedClassHolder o1, MergedClassHolder o2) {
				// if(o1.isDeprecated()) {
				// if(!o2.isDeprecated()) {
				// return 1;
				// }
				// }
				// else if(o2.isDeprecated()) {
				// return -1;
				// }
				String browserText1 = o1.getBrowserText();
				String browserText2 = o2.getBrowserText();
				if (browserText1.startsWith("'")) {
					browserText1 = browserText1.substring(1);
				}
				if (browserText2.startsWith("'")) {
					browserText2 = browserText2.substring(1);
				}
				return browserText1.compareToIgnoreCase(browserText2);
			}
		});
		return result;
	}

	private List<MergedClassHolder> loadAllClassList(OWLAPIProject project) {
		List<MergedClassHolder> result = new ArrayList<MergedClassHolder>();
		Stack<MergedClassHolder> sClassHolders = new Stack<MergedClassHolder>();
		MergedClassHolder root = this.getRootClass(project);
		sClassHolders.push(root);
		while (!sClassHolders.empty()) {
			MergedClassHolder currentClass = sClassHolders.pop();
			List<MergedClassHolder> children = this.getSubclasses(project,
					currentClass.getName());
			Collections.reverse(children);
			for (MergedClassHolder child : children) {
				sClassHolders.push(child);
			}
			result.add(currentClass);
		}
		return result;
	}

	private void fillMappingNo(OWLAPIProject project, List<MergedClassHolder> classHolderList, String refProjectName) {
		OWLDataFactory df = project.getDataFactory();
 		for (MergedClassHolder owlClassHolder: classHolderList){
 			OWLClass owlClass = owlClassHolder.getOwlClass(); 
 			for (OWLAnnotation annotation : owlClass.getAnnotations(project.getRootOntology(), df.getRDFSComment())) {
	    		 if (annotation.getValue() instanceof OWLLiteral) {
		    		 OWLLiteral commentValue = (OWLLiteral) annotation.getValue();
		    		 String annotationValue = commentValue.getLiteral();
		    		 /* 
		    		 if (value.startsWith(UtilConstants.FLAG_MAPPING_NO)){
		    			 int pos = value.indexOf(":");
		    			 String sMappingNo = value.substring(pos+1).trim();
		    			 owlClassHolder.setMappingNo(Integer.parseInt(sMappingNo));
		    		 }
		    		 */
		    		 int mappingNo = MetaItemList.extractPreProjectMappingNumber(annotationValue, refProjectName);
		    		 if (mappingNo!=UtilConstants.UNKNOWN_INT_VALUE){
		    			 owlClassHolder.setMappingNo(mappingNo);
		    		 }
	    		 }//end if
 			}//end for
 			
 		}
	}
	
	private MergedClassHolder getRootClass(OWLAPIProject project) {
		MergedClassHolder holder = new MergedClassHolder();
		RenderingManager rm = project.getRenderingManager();
		OWLClass owlThing = project.getDataFactory().getOWLThing();
		String browserText = rm.getBrowserText(owlThing);
		String name = owlThing.getIRI().toString();
		holder.setBrowserText(browserText);
		holder.setName(name);
		holder.setOwlClass(owlThing);
		return holder;
	}

	public MergenceResult mergeOntology(String projectName, String sourceEntityName,
			MergenceOptions options, UserId userId) {
		// TODO Auto-generated method stub
		_existingO1ClassBrowserTexts = new ArrayList<String>();
		_existingO1IndividualBrowserTexts =  new ArrayList<String>();
		_hEntityName2Result = new HashMap<String, MergeEntityResult>();
		
	
		List<MergedClassHolder> o1ClassList = null;
 		List<MergedClassHolder> o2ClassList = null;
 		List<OWLOntologyChange> o1ChangeList = new ArrayList<OWLOntologyChange>();
 		List<OWLOntologyChange> o2ChangeList = new ArrayList<OWLOntologyChange>();
 		OWLAPIProject project1 = this.getProject(projectName);
 		OWLAPIProject project2 = this.getProject(options.getRefProjectName());
 		OWLOntology o1 = project1.getRootOntology();
 		o1ClassList= this.loadAllClassList(project1);
 		o2ClassList= this.loadAllClassList(project2);
 		this.fillMappingNo(project1, o1ClassList, options.getRefProjectName());
 		this.fillMappingNo(project2, o2ClassList, projectName);
 		resetEmptyMappingNo(o1ClassList, -1);
 		resetEmptyMappingNo(o2ClassList, -2);
 		
 		MergedClassHolderComparator comparator = new MergedClassHolderComparator(options.getMatchMode());
 		Collections.sort(o2ClassList, comparator);
 		HashMap<MergedClassHolder, ArrayList<MergedClassHolder>> hClassMap = new HashMap<MergedClassHolder, ArrayList<MergedClassHolder>>();
 		for (MergedClassHolder o1Class: o1ClassList){
 			String o1ClassBrowserText = o1Class.getBrowserText().toLowerCase();
 			int pos = Collections.binarySearch(this._existingO1ClassBrowserTexts, o1ClassBrowserText);
 			if (pos<0){
 				_existingO1ClassBrowserTexts.add((pos + 1) * -1, o1ClassBrowserText);
 			}
 		}
 		RenderingManager rm1 = project1.getRenderingManager();
 		Set<OWLNamedIndividual> sO1Individuals = o1.getIndividualsInSignature();
 		for (OWLNamedIndividual o1NamedIndividual: sO1Individuals){
 			String o1IndividualBrowserText = rm1.getBrowserText(o1NamedIndividual).toLowerCase();
 			int pos = Collections.binarySearch(this._existingO1IndividualBrowserTexts, o1IndividualBrowserText);
 			if (pos<0){
 				_existingO1IndividualBrowserTexts.add((pos + 1) * -1, o1IndividualBrowserText);
 			}
 		}
 		for (MergedClassHolder o1Class: o1ClassList){
 			if (sourceEntityName!=null && o1Class.getName().compareTo(sourceEntityName)!=0){
 				continue;
 			}
 			ArrayList<Integer> alPositionsOfMatchedO2Classes = new ArrayList<Integer>();
 			int pos = Collections.binarySearch(o2ClassList, o1Class, comparator);
 			if (pos<0){
 				continue;
 			}
 			ArrayList<MergedClassHolder> matchedClassList = new ArrayList<MergedClassHolder>();
 			matchedClassList.add(o2ClassList.get(pos));
 			alPositionsOfMatchedO2Classes.add(pos);
 			for (int i=pos+1; i<o2ClassList.size(); i++){
 				if (comparator.compare(o1Class, o2ClassList.get(i))==0){  
 					matchedClassList.add(o2ClassList.get(i));
 					alPositionsOfMatchedO2Classes.add(i);
 				}else{
 					break;
 				}
 			}
 			for (int i=pos-1; i>=0; i--){
 				if (comparator.compare(o1Class, o2ClassList.get(i))==0){
 					matchedClassList.add(o2ClassList.get(i));
 					alPositionsOfMatchedO2Classes.add(i);
 				}else{
 					break;
 				}
 			}
 			//如果O2中的某个类与O1中的某个类匹配，应将其从o2ClassList中删除，已避免其在与o2ClassList其它类匹配。
 			Collections.sort(alPositionsOfMatchedO2Classes);
 			Collections.reverse(alPositionsOfMatchedO2Classes);
 			for (Integer position: alPositionsOfMatchedO2Classes){
 				o2ClassList.remove(position);
 			}
 			hClassMap.put(o1Class, matchedClassList);
 		}//end for
 		
 		MergenceResult result1  = this.mergeClassMap(hClassMap, project1, project2, options, userId);
 		EntityCounter counter = new EntityCounter();
 		StatisticsResult result2 = counter.countEntities(projectName);
 		result1.setMergedClassNumber(result2.getMergedClassNumber());
 		result1.setMergedIndividualNumber(result2.getMergedIndividualNumber());
 		result1.setMergedPropertyNumber(result2.getMergedPropertyNumber());
 		
 		result1.setOriginalClassNumber(result2.getOriginalClassNumber());
 		result1.setOriginalIndividualNumber(result2.getOriginalIndividualNumber());
 		result1.setOriginalPropertyNumber(result2.getOriginalPropertyNumber());
 		
 		result1.setTotalClassNumber(result2.getTotalClassNumber());
 		result1.setTotalIndividualNumber(result2.getTotalIndividualNumber());
 		result1.setTotalPropertyNumber(result2.getTotalPropertyNumber());
 		return result1;
	}
	private void resetEmptyMappingNo(List<MergedClassHolder> classList, int newMappingNo){
		for (MergedClassHolder classHolder: classList){
			if (classHolder.getMappingNo()==0 || classHolder.getMappingNo()==UtilConstants.UNKNOWN_INT_VALUE){
				classHolder.setMappingNo(newMappingNo);
			}
		}
	}
	private MergenceResult mergeClassMap(HashMap<MergedClassHolder, ArrayList<MergedClassHolder>> hClassMap, OWLAPIProject project1, OWLAPIProject project2, MergenceOptions options,UserId userId){
		MergenceResult result = new MergenceResult();
		for (Entry<MergedClassHolder, ArrayList<MergedClassHolder>> entry : hClassMap.entrySet()) {
			MergedClassHolder o1Class = entry.getKey();
			ArrayList<MergedClassHolder> alMatchedO2ClassList = entry.getValue();
			for (MergenceContent mergenceContent: options.getMergenceContentList()){
				if (mergenceContent == MergenceContent.SUBCLASS){
					this.mergeClass(o1Class, alMatchedO2ClassList, project1, project2, userId);
				}else if (mergenceContent == MergenceContent.INDIVIDUAL){
					this.mergeIndividuals(o1Class, alMatchedO2ClassList, project1, project2, userId);
				}
			}
		}
		return result;
	}
	private void mergeClass(MergedClassHolder o1Class, ArrayList<MergedClassHolder> alMatchedO2ClassList, OWLAPIProject project1, OWLAPIProject project2, UserId userId){
	
		ArrayList<CandidateEntity> alCandidateEntityList = new ArrayList<CandidateEntity>();
		for (MergedClassHolder matchedO2Class: alMatchedO2ClassList){
			List<MergedClassHolder> o2SubClassHolders = this.getSubclasses(project2, matchedO2Class.getName());
			for (MergedClassHolder o2SubClassHolder: o2SubClassHolders){
				int pos = Collections.binarySearch(this._existingO1ClassBrowserTexts, o2SubClassHolder.getBrowserText().toLowerCase());
	 			if (pos>=0){
	 				continue;
	 			}
	 			_existingO1ClassBrowserTexts.add((pos + 1) * -1, o2SubClassHolder.getBrowserText().toLowerCase()); 
	 			CandidateEntity candidateEntity = new CandidateEntity();
	 			candidateEntity.setEntityType(UtilEntityType.CLASS);
	 			candidateEntity.setBrowserText(o2SubClassHolder.getBrowserText());
	 			candidateEntity.setSourceLabel(project2.getProjectId().toString());
	 			alCandidateEntityList.add(candidateEntity);
			}
		}
		
		ArrayList<CandidateClassHolder> alCandidateClassHolders = new ArrayList<CandidateClassHolder>();
 	    CandidateClassHolder candidateClassHolder = new CandidateClassHolder();
 	    candidateClassHolder.setOwlClass(o1Class.getOwlClass());
 	    candidateClassHolder.setCandidateChildClassList(alCandidateEntityList);
 	    alCandidateClassHolders.add(candidateClassHolder);
 	    CandidateClassesChangeGenerator changeGenerator = new CandidateClassesChangeGenerator(alCandidateClassHolders);
 		ChangeDescriptionGenerator descriptionGenerator = new FixedMessageChangeDescriptionGenerator("合并子类");
 		ChangeApplicationResult result1 =  project1.applyChanges(userId, changeGenerator, descriptionGenerator);
			
 		HashMap<OWLClass, String> hFreshClassList = changeGenerator.getFresshClassList();
 		OWLDataFactory df = project1.getDataFactory();
 		List<OWLOntologyChange> changeList = new ArrayList<OWLOntologyChange>();
 		ArrayList<OWLEntity> alRenamedClassList = new ArrayList<OWLEntity>();
 		for (Entry<OWLClass, String> entry : hFreshClassList.entrySet()) {
 			OWLClass freshClass = entry.getKey();
 			String from = entry.getValue();
 			OWLEntity renamedClass = result1.getRenamedEntity(freshClass);
 			String annotaionValue= UtilConstants.FLAG_MERGED + ": Y" + "; " + UtilConstants.FLAG_FROM + ": " + from;
 			OWLAnnotation commentAnno = df.getOWLAnnotation(
 					 df.getRDFSComment(),
 					 df.getOWLLiteral(annotaionValue, "en"));
 			
 			OWLAxiom ax = df.getOWLAnnotationAssertionAxiom(renamedClass.getIRI(), commentAnno);
 			AddAxiom addAxiom =  new AddAxiom(project1.getRootOntology(), ax);	 
 			changeList.add(addAxiom);
 			alRenamedClassList.add(renamedClass);
 		}
 		project1.applyChanges(userId, changeList, "添加annotaion");
 		
 		RenderingManager rm1 = project1.getRenderingManager();
 		ArrayList<SubclassEntityData> subClassEntityDataList = new ArrayList<SubclassEntityData>();
 		for (OWLEntity renamedClass: alRenamedClassList){
 			String browserText = rm1.getBrowserText(renamedClass);
            String name = renamedClass.getIRI().toString();
            SubclassEntityData subEntityData = new SubclassEntityData(name, browserText, Collections.<EntityData>emptySet(), 0);
            subClassEntityDataList.add(subEntityData);
 		}
 		MergeEntityResult mergeEntityResult = this._hEntityName2Result.get(o1Class.getName());
		if (mergeEntityResult==null){
			mergeEntityResult = new MergeEntityResult();
			_hEntityName2Result.put(o1Class.getName(), mergeEntityResult);
		}
		mergeEntityResult.setSubClassEntityDataList(subClassEntityDataList);
	}
	
	private List<EntityData> fetchIndividuals(OWLAPIProject project, String className){
		List<EntityData> result = new ArrayList<EntityData>();
		
		RenderingManager rm = project.getRenderingManager();
        OWLClass cls = rm.getEntity(className, EntityType.CLASS);
        OWLOntology rootOntology = project.getRootOntology();

        if(className.equals(OWLRDFVocabulary.OWL_THING.getIRI().toString())) {
            for (OWLOntology ont : rootOntology.getImportsClosure()) {
                for(OWLNamedIndividual namedIndividual : ont.getIndividualsInSignature()) {
                    result.add(rm.getEntityData(namedIndividual));
                }
            }
        }
        else {
            for(OWLIndividual individual : cls.getIndividuals(rootOntology.getImportsClosure())) {
                EntityData entityData = null;
                if(individual.isAnonymous()) {
                   // entityData = rm.getEntityData(individual.asOWLAnonymousIndividual());
                }
                else {
                    entityData = rm.getEntityData(individual.asOWLNamedIndividual());
                    result.add(entityData);
                }
                
            }
        }

        return result;
	}
	private void mergeIndividuals(MergedClassHolder o1Class, ArrayList<MergedClassHolder> alMatchedO2ClassList, OWLAPIProject project1, OWLAPIProject project2, UserId userId){
		ArrayList<CandidateEntity> alCandidateEntityList = new ArrayList<CandidateEntity>();
		for (MergedClassHolder matchedO2ClassHolder: alMatchedO2ClassList){
			List<EntityData> lIndividuals = this.fetchIndividuals(project2, matchedO2ClassHolder.getName());
			for (EntityData o2Individual: lIndividuals){
				int pos = Collections.binarySearch(this._existingO1IndividualBrowserTexts, o2Individual.getBrowserText().toLowerCase());
	 			if (pos>=0){
	 				continue;
	 			}
	 			_existingO1IndividualBrowserTexts.add((pos + 1) * -1, o2Individual.getBrowserText().toLowerCase()); 
	 			CandidateEntity candidateEntity = new CandidateEntity();
	 			candidateEntity.setEntityType(UtilEntityType.NAMED_INDIVIDUAL);
	 			candidateEntity.setBrowserText(o2Individual.getBrowserText());
	 			candidateEntity.setSourceLabel(project2.getProjectId().toString());
	 			alCandidateEntityList.add(candidateEntity);
			}
		}
		
		ArrayList<CandidateClassHolder> alCandidateClassHolders = new ArrayList<CandidateClassHolder>();
 	    CandidateClassHolder candidateClassHolder = new CandidateClassHolder();
 	    candidateClassHolder.setOwlClass(o1Class.getOwlClass());
 	    candidateClassHolder.setCandidateChildClassList(alCandidateEntityList);
 	    alCandidateClassHolders.add(candidateClassHolder);
 	    CandidateIndividualsChangeGenerator changeGenerator = new CandidateIndividualsChangeGenerator(alCandidateClassHolders);
 		ChangeDescriptionGenerator descriptionGenerator = new FixedMessageChangeDescriptionGenerator("合并个体");
 		ChangeApplicationResult result1 =  project1.applyChanges(userId, changeGenerator, descriptionGenerator);
			
 		HashMap<OWLNamedIndividual, String> hFreshEntityList = changeGenerator.getFresshIndividualList();
 		OWLDataFactory df = project1.getDataFactory();
 		List<OWLOntologyChange> changeList = new ArrayList<OWLOntologyChange>();
 		ArrayList<OWLEntity> alRenamedEntityList = new ArrayList<OWLEntity>();
 		for (Entry<OWLNamedIndividual, String> entry : hFreshEntityList.entrySet()) {
 			OWLNamedIndividual freshEntity = entry.getKey();
 			String from = entry.getValue();
 			OWLEntity renamedEntity = result1.getRenamedEntity(freshEntity);
 			String annotaionValue= UtilConstants.FLAG_MERGED + ": Y" + "; " + UtilConstants.FLAG_FROM + ": " + from;
 			OWLAnnotation commentAnno = df.getOWLAnnotation(
 					 df.getRDFSComment(),
 					 df.getOWLLiteral(annotaionValue, "en"));
 			
 			OWLAxiom ax = df.getOWLAnnotationAssertionAxiom(renamedEntity.getIRI(), commentAnno);
 			AddAxiom addAxiom =  new AddAxiom(project1.getRootOntology(), ax);	 
 			changeList.add(addAxiom);
 			alRenamedEntityList.add(renamedEntity);
 		}
 		project1.applyChanges(userId, changeList, "添加annotaion");
 		
 		RenderingManager rm1 = project1.getRenderingManager();
 		ArrayList<EntityData> freshEntityDataList = new ArrayList<EntityData>();
 		for (OWLEntity renamedEntity: alRenamedEntityList){
 			String browserText = rm1.getBrowserText(renamedEntity);
            String name = renamedEntity.getIRI().toString();
            EntityData freshEntityData = new EntityData(name, browserText, Collections.<EntityData>emptySet());
            freshEntityDataList.add(freshEntityData);
 		}
 		
 		MergeEntityResult mergeEntityResult = this._hEntityName2Result.get(o1Class.getName());
		if (mergeEntityResult==null){
			mergeEntityResult = new MergeEntityResult();
			_hEntityName2Result.put(o1Class.getName(), mergeEntityResult);
		}
		mergeEntityResult.setNamedIndividualDataList(freshEntityDataList);
	}
	public MergeEntityResult getMergeEntityResult(String entityName){
		MergeEntityResult result = this._hEntityName2Result.get(entityName);
		if (result!=null){
			return result;
		}else{
			return new MergeEntityResult();
		}
	}
}
